<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 采购单
 * 催货处理
 */

class Controller_purchases_expediting extends Template{


  public function Action_stockchase()
  {

		try{
      ACL::Required('purchases_expediting');
    }catch(HTTP_Exception_403 $e){
			Message::error('sorry，你没有权限进这个目录');
      $this->request->redirect(URL::base(true));
    }
  	$this->title=__("未到货催货");

  		$purchase_lists = NULL;
  		$condition = array();

  		//根据采购员的名称查询
  		if(isset($_GET['purchase_id_name']) and trim($_GET['purchase_id_name']))
  		{
  			$condition['purchase_id_name'] = trim($_GET['purchase_id_name']);
  		}
  		else 
  		{
  			$condition['purchase_id_name'] = user::active_user()->nick;		
  		}
  		
  		//采购单时间
  		if(isset($_GET['purchaseorder_date_from']))
  		{
  				$condition['purchaseorder_date_from'] = $_GET['purchaseorder_date_from'];
  		}
  		
  		if (isset($_GET['purchaseorder_date_to'])) {
  				
  				$condition['purchaseorder_date_to'] = $_GET['purchaseorder_date_to'];
  		}
  		else {
  			$condition['purchaseorder_date_to'] = date('Y-m-d');
  		}
  			

  		//交期
  		if(isset($_GET['arrive_date_from']))
  		{
  				$condition['arrive_date_from'] = $_GET['arrive_date_from'];
  		}
  		
  		if (isset($_GET['arrive_date_to'])) {
  				$condition['arrive_date_to'] = $_GET['arrive_date_to'];
  		}
  		else
  		{
  				$condition['arrive_date_to'] = date('Y-m-d');
  		}	
  		

  		if(isset($_GET['sku']) and trim($_GET['sku']))
  		{
  			$condition['sku'] = trim($_GET['sku']);
  		}
  		
  		if(isset($_GET['supplier_name']) and trim($_GET['supplier_name']))
  		{
  			$condition['supplier_name'] = $_GET['supplier_name'];
  		}

  		if(isset($_GET['order_by_sel']))
  		{
  			$condition['order_by_sel'] = $_GET['order_by_sel'];
  		}
  		//是否催货
    	if(isset($_GET['expediting_remark']))
  		{
  			$condition['expediting_remark'] = $_GET['expediting_remark'];
  		}
 
  	if($this->get_purchase_stockchase($condition))
  	{
  		$count = count($this->get_purchase_stockchase($condition)->execute('newerp'));
  		//var_dump($this->get_purchase_stockchase($condition)->execute('newerp'));
  		//分页
	    $pagination         = Pagination::factory(array(
	      'items_per_page'  => 50, 
	      'total_items'     => $count,
	      'uri'	            => $this->request->uri(),
	    ));


  		$purchase_lists = $this->get_purchase_stockchase($condition)
  		->offset($pagination->_offset)
  		->limit($pagination->items_per_page)
  		->execute('newerp')
  		->as_array();
  	
  	}
//	echo "<pre>";
//	print_r($purchase_lists);
//	echo "</pre>";exit;
  	$view = View::factory('purchase/stockchase');
  	$view->set('purchase_lists',$purchase_lists);
  	$view->set('GET',$condition);
  	$view->set('pagination',$pagination);
  	$this->response->body($view);
  }

  //查询未完成的
  protected function get_purchase_stockchase(array $condition)
  {

		try{
      ACL::Required('purchases_expediting');
    }catch(HTTP_Exception_403 $e){
			Message::error('sorry，你没有权限进这个目录');
      $this->request->redirect(URL::base(true));
    }

  	$purchase_lists = DB::select(array('purchaseorder_details.id','details_id'),'purchaseorders.id',
  			'purchaseorder_details.quantity','purchaseorder_details.recieve_quantity','purchaseorder_details.purchaseorder_id',
  			'purchaseorder_details.sku','purchaseorder_details.sku_name','purchaseorder_details.supplier','purchaseorders.purchase_id',
  			'purchaseorders.purchaseorder_date','purchaseorder_details.arrive_date','purchaseorder_details.expediting_remark','purchaseorder_details.ware_quantity')
  	->from('purchaseorder_details')
  	->join('purchaseorders')
  	->on('purchaseorder_details.purchaseorder_id','=','purchaseorders.id')
  	->where('purchaseorder_details.status','in',array(10,20,30,40,50,60,65))
  	->where('purchaseorder_details.quantity','>',DB::expr('ifnull(purchaseorder_details.ware_quantity,0)'));
  	if($condition)
  	{

  		if(isset($condition['purchase_id_name']))
  		{
  			$purchase_id = ORM::factory('user')->where('nick','LIKE',$condition['purchase_id_name'])->find();
  			if($purchase_id->loaded())
  			{
  				$condition['purchase_id'] = $purchase_id->id;
  			}
  			else 
  			{
  				return NULL;
  			}
  		}

  		if(isset($condition['purchaseorder_date_from']) and trim($condition['purchaseorder_date_from']))
  		{
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorders.purchaseorder_date','>=',trim($condition['purchaseorder_date_from']));
  		}

  		if(isset($condition['purchaseorder_date_to']) and trim($condition['purchaseorder_date_to']))
  		{
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorders.purchaseorder_date','<=',trim($condition['purchaseorder_date_to'])." 23:59:59");
  		}

  		if(isset($condition['arrive_date_from']) and trim($condition['arrive_date_from']))
  		{
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorder_details.arrive_date','>=',trim($condition['arrive_date_from']));
  		}

  		if(isset($condition['arrive_date_to']) and trim($condition['arrive_date_to']))
  		{
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorder_details.arrive_date','<=',trim($condition['arrive_date_to']));
  		}
  		//是否已经催货
  	  	if(isset($condition['expediting_remark']) and trim($condition['expediting_remark']))
  		{
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorder_details.expediting_remark','=',$condition['expediting_remark']);
  		}
  		
  		if(isset($condition['supplier_name']) and trim($condition['supplier_name']))
  		{
//          ----------------------------kdw---------------------------
//  			$supplier_lists = DB::select(array('erp_provider.id','id'))
//  			->from('erp_provider')
//  			->where('vProviderName','LIKE','%'.trim($condition['supplier_name']).'%')
//  			->execute('readonlyerp')
//  			->as_array();
  			//var_dump($supplier_lists);exit;

            $fields=array('id');
            $supplier_name=trim($condition['supplier_name']);
            $condition="LIKE '%".$supplier_name."%'";
            $condtions=array('name'=>$condition);
            $supplier_lists=Newerpclientapi::getSuppliers($fields,$condtions);
            //-------------------------kdw-end-----------------------


            if($supplier_lists and count($supplier_lists)>0)
  			{
  				$purchase_lists = 
  					$purchase_lists->where('purchaseorders.supplier_id','IN',array_values($supplier_lists));
  			}
  			else{
  				return NULL;
  			}
  		}
  		if(isset($condition['sku']) and trim($condition['sku']))
  		{
  			// echo "123";exit;
  			$purchase_lists = 
  			$purchase_lists->where('purchaseorder_details.sku','=',trim($condition['sku']));
  		}

  		if(isset($condition['order_by_sel']))
  		{
  			if($condition['order_by_sel'] == 1)
  			{
  				$purchase_lists = 
  					$purchase_lists->order_by('purchaseorder_details.details_date');
  			}
  			elseif($condition['order_by_sel'] == 2)
  			{
  				$purchase_lists = 
  					$purchase_lists->order_by('purchaseorder_details.arrive_date');
  			}
  			else
  			{
  				$purchase_lists = 
  					$purchase_lists->order_by('purchaseorder_details.details_date');
  			}
  		}

  		if(isset($condition['purchase_id']))
  		{
  			$purchase_lists = 
  					$purchase_lists->where('purchaseorders.purchase_id','=',$condition['purchase_id']);
  		}
  	}
 		//print_r($purchase_lists->execute('newerp'));exit;
  		return $purchase_lists;//->execute('newerp')->as_array();

  }

   public function Action_add_expediting_remark()
  {
  		$re = false;

  	try{
	  	ACL::Required('expediting edit');
	  	}
	 			catch(HTTP_Exception_403 $e){
	 		echo json_encode($re);exit;
	 	}
		
  		if(isset($_POST['details_id']))	
  		{
  			
  			$re = DB::update('purchaseorder_details')
  			->set(array('expediting_remark'=>20))
  			->where('id','=',$_POST['details_id'])
  			->execute('newerp');
  			// $re = $_POST['data']['expediting_remark'];
  		}

  		echo json_encode($re);exit;
  } 

}
